package com.customization.unionpay.market.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.util.FormDataUtils;
import com.customization.unionpay.market.service.MarketAdvanceRefundCheckWorkflowService;
import weaver.backup.logging.Logger;
import weaver.backup.logging.LoggerFactory;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class MarketAdvanceRefundCheckWorkflowServiceImpl implements MarketAdvanceRefundCheckWorkflowService {
    private  static final Logger log = LoggerFactory.getLogger(MarketAdvanceRefundCheckWorkflowServiceImpl.class);
    @Override
    public List<JSONObject> checkBudget(String action, RequestInfo requestInfo) {
        try {
            //获取主表数据
            Map<String,Object> map = com.customization.unionpay.market.util.FormDataUtils.getMainFormData(requestInfo);
            //项目编码
            String xmbm = Util.null2String(map.get("PROJECT_CODE"));
            //预算控制期
            String yskzq = Util.null2String(map.get("yskzq"));
            //金额
            String je = Util.null2String(map.get("bcyfthje"));
            //表单号
            String bdh = Util.null2String(map.get("bdh"));
            //业务类型
            String ywlx = Util.null2String(map.get("ywlx"));
            //申请人
            String sqr = Util.null2String(map.get("sqr"));
            //报账公司
            String bzgs = Util.null2String(map.get("bzgs"));
            //报账部门
            String bzbm = Util.null2String(map.get("bzbm"));
            //入账日期
            String rzrq = Util.null2String(map.get("rzrq"));
            //说明
            String sm = "";
            // 获取业务类型的中文名称
            List<JSONObject> ywlxName = com.customization.unionpay.market.util.FormDataUtils.getItemInfo(requestInfo.getRequestManager().getFormid(),
                    "ywlx", null, ywlx);
            if (CollectionUtil.isEmpty(ywlxName)) {
                log.error("业务类型为空");
                return null;
            }

            //获取申请人名称
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            String sqrName = resourceComInfo.getLastname(sqr);
            //获取部门的名称
            DepartmentComInfo departmentComInfo = new DepartmentComInfo();
            String bzbmName = departmentComInfo.getDepartmentname(bzbm);
            //获取公司的名称
            SubCompanyComInfo subCompanyComInfo = new SubCompanyComInfo();
            String bzgsName = subCompanyComInfo.getSubCompanyname(bzgs);
            //获取明细表数据
            DetailTable tableInfo = requestInfo.getDetailTableInfo().getDetailTable(0);
            List<Map<String,Object>>  jjsxList = com.customization.unionpay.market.util.FormDataUtils.getDetailFormDataByIndex(0,requestInfo);
            if (CollectionUtil.isEmpty(jjsxList)) {
                log.error("预付款单明细表数据为空");
                return null;
            }
            List<JSONObject> list = new ArrayList<>();
            for (Map<String, Object> jjsx : jjsxList) {

                JSONObject rowData = new JSONObject();
                //预算责任中心
                rowData.put("yszrzx",Util.null2String(jjsx.get("yszrzx")));
                //预算科目
                rowData.put("yskm",Util.null2String(jjsx.get("yskm")));
                //预付金额
                rowData.put("yfje",Util.null2String(jjsx.get("yfje")));
                //经济事项
                rowData.put("jjsx",Util.null2String(jjsx.get("jjsx")));
                list.add(rowData);
            }

            List<Map<String,Object>>  hxyfmxList = FormDataUtils.getDetailFormDataByIndex(1,requestInfo);
            if (CollectionUtil.isEmpty(hxyfmxList)) {
                log.error("核销预付款明细表数据为空");
                return null;
            }
            //预算占用请求列表
            List<JSONObject> budgetOccupyRequests = new ArrayList<>();
            //调用预算接口
            for (JSONObject rowData : list) {
                JSONObject jsonObject = new JSONObject();
                //预算项目
                jsonObject.put("budgetItem", xmbm);
                //预算科目
                jsonObject.put("budgetAccount", rowData.getString("yskm"));
                //预算责任中心
                jsonObject.put("exeUnit", rowData.getString("yszrzx"));
                //预算期间(年)
                jsonObject.put("budgetPeriod", yskzq.substring(0, 4));
                //金额
                jsonObject.put("amount", je);
                //操作
                jsonObject.put("instruction", action);
                //已占据单号
                //执行单据号
                jsonObject.put("exeDocNo", xmbm);
                //预算控制期
                jsonObject.put("budgetPostingPeriod", yskzq);
                //执行日期(当前日期)
                jsonObject.put("executionDate", DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN));
                //执行单据类型
                jsonObject.put("exeDocType", ywlxName.get(0).getString("name"));
                //报账人
                jsonObject.put("claimant", sqrName);
                //来源系统
                jsonObject.put("sourceSystem", "智慧财务平台");
                //说明
                jsonObject.put("remark", "");
                //报账部门
                jsonObject.put("reimbursementDept", bzbmName);
                //报账公司
                jsonObject.put("reimbursementCompany", bzgsName);
                //经济事项
                jsonObject.put("economicEvent", rowData.getString("jjsx"));
                budgetOccupyRequests.add(jsonObject);
            }
            return budgetOccupyRequests;
        }catch (Exception e){
            return null;
        }
    }
}
